﻿using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {

	public int damage = 1;
	public float timeBetweenAttacks = 0.5f;
	public float range = 0.30f;
	
	float timer;
	private ArrayList enemiesInRange;
	
	
	void Awake ()
	{
		enemiesInRange = new ArrayList ();
	}
	
	void Update ()
	{
		timer += Time.deltaTime;
		
		if(Input.GetButton ("Fire1") && timer >= timeBetweenAttacks)
		{
			Attack ();
		}
	}
	
	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.gameObject.name == "Enemy")
		{
			if(!enemiesInRange.Contains(other.gameObject))
			{
				enemiesInRange.Add(other.gameObject);
			}
			Debug.Log("Hej Fjende" + other.name);
		}
	}

	void OnTriggerExt2D(Collider2D other)
	{
		if (other.gameObject.name == "Enemy")
		{
			enemiesInRange.Remove(other.gameObject);
			Debug.Log("Farvel Fjende" + other.name);
		}

	}
	
	
	void Attack ()
	{
		Debug.Log ("Attacked");
		timer = 0f;
		foreach (GameObject enemy in enemiesInRange) 
		{
			enemy.GetComponent<EnemyHealth>().TakeDamage(damage);
		}

	}
}